package com.yutian.example.demo.web.config;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;


/**
 * <pre>
 * 描述：切面记录请求日志，拦截各种接口
 * </pre>
 *
 * @author zhoulei7
 * @since 1.0
 */
@Slf4j
@Aspect
@Component
@Order()
public class ControllerAspect {
    @Pointcut("execution(* com.yutian.example.demo.web..*Controller.*(..))")
    public void api() {
    }
    @Around("api()")
    public Object invoke(ProceedingJoinPoint pjp) throws Throwable {
        long begin = System.currentTimeMillis();
        Class<?> clazz = pjp.getSignature().getDeclaringType();
        String className = clazz.getName();
        Signature signature = pjp.getSignature();
        String methodName = signature.getName();
        Object rsp = pjp.proceed();
        log.info("{}::{}, cost time(ms) = {}", className, methodName, System.currentTimeMillis() - begin);
        return rsp;
    }
}
